package xixirupan.tmgp.tencent.com.vmeim.presenter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import java.util.List;

import xixirupan.tmgp.tencent.com.vmeim.R;
import xixirupan.tmgp.tencent.com.vmeim.model.FragmentFactory;
import xixirupan.tmgp.tencent.com.vmeim.model.IGetFragments;
import xixirupan.tmgp.tencent.com.vmeim.view.IGetViewData;

/**
 * Created by lenovo on 2016/10/18.
 */

public class GetFragmentsPresenter  {
    FragmentFactory factory;
    IGetViewData getViewData;
    int currentIndex;
    List<Fragment> lists;

    public GetFragmentsPresenter( FragmentFactory factory,IGetViewData viewData,int currentIndex)
    {

        this.factory =factory;
        this.getViewData =viewData;
        this.currentIndex =currentIndex;
       lists = factory.initFragements();
    }
    //参数是下标
    public  void switchFragment(int index){

        //按下按钮对应的fragment
        Fragment fragment =lists.get(index);//获取点击按钮对应的fragment
        //获取点击按钮之前的当前fragemnt
        Fragment currentFragment = lists.get(currentIndex);
        //获取fragment的事务
        FragmentTransaction transaction = getViewData.getManager().beginTransaction();
        //如果fragment已经被添加了
        //public final boolean isAdded()
        // 如果该Fragment对象被添加到了它的Activity中,
        // 那么它返回true,否则返回false。
        if(fragment.isAdded())
        {
            transaction.hide(currentFragment).show(fragment);
        }else {
            //隐藏当前fragment 添加目标fragment

            transaction.hide(currentFragment).add(R.id.frameLayout_main,fragment);
        }
        //然后提交
        transaction.commit();
        currentIndex =index;

    }
    public void setInitView()
    {
        //initFragements();
        //默认加载第一个fragment
       getViewData.getManager()
                .beginTransaction()
                .add(R.id.frameLayout_main,lists.get(0))
                .commit();
    }


}
